<?php

namespace app\merchant\controller;

use app\common\controller\Tools;
use app\common\model\Store as StoreModel;
use app\common\model\GoodsCategory;
use app\common\model\Goods;
use think\Controller;
use think\Db;
use app\common\tool\OSSUtils;

class Store extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!session('merchant')) {
            $this->redirect('index/login');
        }
    }

    public function create()
    {
        $mainCategory = GoodsCategory::getMainCategory();
        $catData = GoodsCategory::getTree();
        $enterpriseId = Db::name('enterprise')
            ->where('enterprise_merchant_id', session('merchant.merchant_id'))
            ->where('enterprise_is_delete', 0)
            ->value('enterprise_id');
        if (empty($enterpriseId)) {
            $this->redirect('enterprise/create');
        }
        $storeId = Db::name('store')
            ->where('store_merchant_id', session('merchant.merchant_id'))
            ->where('store_is_delete', 0)
            ->value('store_id');
        if (!empty($storeId)) {
            $this->redirect('store/update', ['store_id' => $storeId]);;
        }
        return view('create', [
            'mainCategory' => $mainCategory,
            'catData' => $catData,
            'enterprise_id' => $enterpriseId
        ]);
    }

    public function insert()
    {
        $tool = new Tools;
        $data = request()->except('__token__,remLen');
        return $tool->insert('store', $data, 'insert', 'isPost');
    }

    public function update()
    {
        $store_id = input('param.store_id');
        $store = StoreModel::find($store_id);
        if (request()->isPost()) {
            $data = request()->except('__token__');
            $model = new StoreModel;
            $validate = $model->intValidate();
            if (!($validate->scene('update')->check($data)))
                return returnJson(0, $validate->getError());
            if ($store->isUpdate(true)->save($data) !== false) {
                return returnJson(1, '修改成功');
            } else {
                return returnJson(0, '修改失败，请稍后重试！');
            }
        }
        // 主分类
        $mainCategory = GoodsCategory::getMainCategory();
        $catData = GoodsCategory::getTree();
        $enterpriseId = Db::name('enterprise')
            ->where('enterprise_merchant_id', session('merchant.merchant_id'))
            ->where('enterprise_is_delete', 0)
            ->value('enterprise_id');
        return view('update', ['store' => $store, 'mainCategory' => $mainCategory, 'catData' => $catData, 'enterprise_id' => $enterpriseId]);
    }

    public function upload_face_img()
    {
        // 打开图片的相对路径
        //$imgpath = config('img_path');
        $imgRootPath = config('oss_imgRootPath');
        $filename = Goods::makeFileName();
        $whereName = 'store/face';
        $type = Goods::upload($whereName, $filename);
        if ($type) {
            //$savepath = $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type;
            //$thumbfile = session('merchant.merchant_id') . '_store_face_img_130_130' . '.' . $type;
            //$thumbName = $imgpath . '/' . $whereName . '/' . $thumbfile;

            //$image = \think\Image::open($imgpath . '/store/face/' . $filename . '.' . $type);
            //$image->thumb(130, 130, \think\Image::THUMB_FIXED)->save($thumbName);

            $data = [
                'access_url' => $imgRootPath . $whereName . '/' . $filename . '.' . $type,
                //'filename' => $thumbfile,
                'filename' => $filename . '.' . $type,
            ];

            return returnJson(1, '上传成功', $data);
        }

    }

    public function upload_bg_img()
    {
        // 打开图片的相对路径
        // halt($rootpath);
        //$imgpath = config('img_path');
        // 店铺编号+商户编号+时间戳+随机4位数
        $imgRootPath = config('oss_imgRootPath');
        $filename = Goods::makeFileName();
        // halt($filename);
        $whereName = 'store/bg';
        $type = Goods::upload($whereName, $filename);
        if ($type) {
            //$savepath = $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type;
            //$thumbfile = session('merchant.merchant_id') . '_store_bg_img_750_360' . '.' . $type;
            //$thumbName = $imgpath . '/' . $whereName . '/' . $thumbfile;

            //$image = \think\Image::open($imgpath . '/store/bg/' . $filename . '.' . $type);
            //$image->thumb(750, 230, \think\Image::THUMB_FIXED)->save($thumbName);
            $data = [
                'access_url' => $imgRootPath . $whereName . '/' . $filename . '.' . $type,
                //'filename' => $thumbfile,
                'filename' => $filename . '.' . $type,
            ];

            return returnJson(1, '上传成功', $data);
        }
    }
}